define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",  
        "text!templates/hall/hall.html",
        "models/aula",
        "models/checkin",
        "models/session",
        "models/utente",
        "collections/orariocorsocollection",
        "collections/checkincollection",
        "views/hall/upeventsview",
        "views/hall/ufoundsview",
        "views/common/menuview"        
], function($, _, Backbone,  Handlebars, template, Aula, Checkin, Session, Utente, OrariocorsoCollection,
		CheckinCollection, UpeventView, UfoundsView, MenuView) {
	
   
	  var AulaPage = Backbone.View.extend({
		  
	        template: Handlebars.compile(template),
	        

	        render: function(eventName) {
	        	
	        	
	        	this.$el.html(this.template(this.model.toJSON()));
	        	
	        	// menu top
	        	this.menutop = new MenuView({
	               	el : $('#menu', this.el),
	    			model : this.model
				});
	            this.menutop.render();
	        	
	        	var d = new Date();
	        	// n numero del giorno della settimana
				var weekday = ["Dom","Lun","Mar","Mer","Gio","Ven","Sab"];
				var day = weekday[d.getDay()];
				
	        	hours = d.getHours();
	            minutes = d.getMinutes();
	        	//time = hours + ":" + minutes;
	        	var oraCorrente = parseInt(hours) + parseInt(minutes)/60;
	        	
	        	this.orarioCollection = new OrariocorsoCollection();
					this.orarioCollection.fetch({
						query : {
							orainizio : {"$gte": oraCorrente},
							weekday: day,
							aula: {"__type":"Pointer","className":"aula","objectId":this.model.get('objectId')}
						},
	                	include : 'corso',          //Include un oggetto di tipo Puntatore
						success: function(data) {
							this.upevents = new UpeventView({	    				
								el : $('#events ul', this.el),
								model : data
							});
							this.upevents.render();
						}
					});
					
					this.checkinCollection = new CheckinCollection();
					// temp
					var h = parseInt(hours);
					var i = parseInt(hours) - 2;
					
					// fuso orario diff. 2 hours
					var dat = new Date();
					dat.setHours(dat.getHours() - 2);
					dat.toISOString();	
					
					this.checkinCollection.fetch({
						query : {
							//orario : {"$gte": i, "$lte": h},
							createdAt: {"$gte": {"__type": "Date", "iso": dat}},
							aula: {"__type":"Pointer","className":"aula","objectId":this.model.get('objectId')}
						},
	                	include : 'utente',          //Include un oggetto di tipo Puntatore
						success: function(data) {
							$('#nchecked').html(data.length);
							this.ufounds = new UfoundsView({	    				
								el : $('#ufounds ul', this.el),
								model : data
							});
							this.ufounds.render();
						}
					});
					
					
	        
	            return this;
	        },
	        
	        events: {
	        	'click #checkin': 'checkin',
	            'touchstart .button': 'over',
	            'touchend .button': 'left',
	            'touchcancel .button': 'left',
	            'touchstart button': 'over',
	            'touchend button': 'left',
	            'touchcancel button': 'left',
	            'touchstart .ac-container article.selectable ul li': 'over',
	            'touchend .ac-container article.selectable ul li': 'left',
	            'touchcancel .ac-container article.selectable ul li': 'left'
	        },


	        checkin: function(event) {
	        	
	        	if(Session.getAuth()){
	        		
	        		var user = new Utente(Session.getAuth());
	        		this.cCollection = new CheckinCollection();
	        		this.cCollection.fetch({
						query : {
							aula: {"__type":"Pointer","className":"aula","objectId":this.model.get('objectId')},
							utente: {"__type":"Pointer","className":"_User","objectId":user.get('objectId')}
						},
						include: 'utente, aula',
	                	success: function(data) {
	                	
	                		/*
	                		_.each(data.models, function(d) {
	                			
	                			var cuser = new Checkin({objectId: d.get('objectId')});
	                			cuser.fetch({
	                                success: function(data) {
	                                			cuser.destroy();
	                                },
	                                error: function(){
	                                	alert('error');
	                                }
	                            });
	                			
	                			﻿  ﻿  ﻿  }, this);
	                		*/
	                		
						}
					});
	        		var c = new Checkin();
	        		var dated = new Date();
	        		dated.setHours(dated.getHours());
	        		var hours = dated.getHours();
	        		var minutes = dated.getMinutes();
	        		var orario = hours + ':' + minutes;
					
	        		c.save({
	        			orario: orario,
	        			aula: {"__type":"Pointer","className":"aula","objectId":this.model.get('objectId')},
	        			utente: {"__type":"Pointer","className":"_User","objectId":user.get('objectId')}
	        		},{
	        			success: function(model, resp){
	        				alert('Registrazione aula');
	        			},
	        			error: function(model, resp){
	        				alert('Error');
	        			}
	        		});
	        	}
	        },
	        
	        
	        over: function(event) {
	            $(event.currentTarget).addClass('fake-active');
	        },

	        left: function(event) {
	            $(event.currentTarget).removeClass('fake-active');
	        }

	});

    return AulaPage;

});

